package com.zys.el.user.util.html;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class comput {
    public  Map<String, Date> getNew() throws IOException, ParseException {
        String url = "http://jsjds.blcu.edu.cn/dsdt.htm";
        Document doc = Jsoup.connect(url).get();
//        System.out.println(doc);
        Elements items = doc.select("li");
        Elements items2 = doc.select("i");
        List<String> announcements = new ArrayList<>();

        for (Element item : items) {

            String announcement = item.selectFirst("a").text(); //+ " " + item.selectFirst("i").text();
            if (announcement.length() < 10) continue;
            announcements.add(announcement);
        }
//        System.out.println(announcements.size());


        List<Date> time = new ArrayList<>();
        for (Element element : items2) {
            String announcement = element.selectFirst("i").text(); //+ " " + item.selectFirst("i").text();
//            if (announcement.length() < 10) continue;

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            //Date转String
            Date date = new Date();
            String now = sdf.format(date);
            //String转Date
            Date now2 = sdf.parse(announcement);
            time.add(now2);
        }
//        System.out.println(time.size());
        Map<String, Date> re = new HashMap<>();
        for (int i = 0 ; i < time.size(); i++) {
            re.put(announcements.get(i), time.get(i));
        }

        return  re;
    }

//    public static void main(String[] args) throws IOException, ParseException {
////        String url = "http://jsjds.blcu.edu.cn/dsdt.htm";
////        Document doc = Jsoup.connect(url).get();
////        System.out.println(doc);
////        Elements items = doc.select("li");
////        Elements items2 = doc.select("i");
//////        System.out.println(items);
////        List<String> announcements = new ArrayList<>();
////
//////        System.out.println(items2);
////        for (Element item : items) {
////
////            String announcement = item.selectFirst("a").text(); //+ " " + item.selectFirst("i").text();
////            if (announcement.length() < 10) continue;
////            announcements.add(announcement);
////        }
////        System.out.println(announcements.size());
////
////
////        List<Date> time = new ArrayList<>();
////        for (Element element : items2) {
////            String announcement = element.selectFirst("i").text(); //+ " " + item.selectFirst("i").text();
//////            if (announcement.length() < 10) continue;
////
////            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
////            //Date转String
////            Date date = new Date();
////            String now = sdf.format(date);
////            //String转Date
////            Date now2 = sdf.parse(announcement);
////            time.add(now2);
////        }
////        System.out.println(time);
//        Map<String, Date> aNew = comput.getNew();
//        System.out.println(aNew);
//    }
}
